<!--
 * @Author: cgl
 * @Date: 2023-04-18 15:12:26
 * @LastEditors: cgl
 * @LastEditTime: 2023-08-07 20:16:46
 * @FilePath: \public-health-emergencies-fed:\radiation-hygiene-fe\src\views\basicData\manufacture.vue
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
-->
<template>
  <div class="app-container">
    <el-form
      class="searchform"
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="100px"
    >
      <el-row>
        <el-col :span="5">
          <el-form-item label="厂家名称" label-width="80px">
            <el-input
              v-model="queryParams.manufactureName"
              placeholder="请输入"
              clearable
              @keyup.enter.native="getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="queryParams.formDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              clearable
              style="width: 100%"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="table-wrapper">
      <div class="tableoperation">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click.native.prevent="openDialog()"
          >添加</el-button
        >
      </div>

      <el-table
        v-loading="loading"
        :data="manufactureList"
        :height="height - 400 + 'px'"
        @row-dblclick="(row) => openDialog(row, '3')"
      >
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column
          label="厂家名称"
          align="center"
          prop="manufactureName"
          width="200"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="简称"
          align="center"
          prop="manufacturerAbbreviation"
          width="200"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="地址" align="center" width="300">
          <template slot-scope="scope">
            <el-tooltip
              :disabled="scope.row.address && scope.row.address.length <= 10"
              placement="top"
              :content="scope.row.address"
            >
              <span>{{ scope.row.address | ellipsis }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          label="联系人"
          align="center"
          prop="contactsName"
          width="120"
        />
        <el-table-column
          label="联系电话"
          align="center"
          prop="contactsMobile"
        />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{ row }">
            <el-button @click.native.prevent="openDialog(row)" type="text">
              编辑
            </el-button>
            <el-button
              @click.native.prevent="handleDelete(row)"
              type="text"
              style="color: #f32d2d"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="1000px"
      v-dialogDrag="true"
    >
      <el-form
        :model="form"
        :disabled="status == '3'"
        :rules="rules"
        ref="form"
        size="small"
        label-width="80px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="名称" prop="manufactureName">
              <el-input
                v-model="form.manufactureName"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="简称">
              <el-input
                v-model="form.manufacturerAbbreviation"
                :placeholder="status != '3' ? '请输入' : ''"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人">
              <el-input
                v-model="form.contactsName"
                :placeholder="status != '3' ? '请输入' : ''"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="contactsMobile">
              <el-input
                v-model="form.contactsMobile"
                :placeholder="status != '3' ? '请输入' : ''"
                clearable
              />
            </el-form-item>
          </el-col>

          <span v-if="status != '3'">
            <el-col :span="7">
              <el-form-item label="单位地址" prop="provinceCode">
                <el-select
                  v-model="form.provinceCode"
                  placeholder="省"
                  style="width: 100%"
                  @change="changeProvince"
                >
                  <el-option
                    :label="item.label"
                    :value="item.id"
                    v-for="(item, index) in provinceList"
                    :key="index"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="" label-width="10px" prop="">
                <el-select
                  v-model="form.cityCode"
                  placeholder="市"
                  style="width: 110%"
                  @change="changeCity"
                >
                  <el-option
                    :label="item.label"
                    :value="item.id"
                    v-for="(item, index) in cityList"
                    :key="index"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="" label-width="20px" prop="">
                <el-select
                  v-model="form.areaCode"
                  placeholder="区县"
                  style="width: 128%"
                >
                  <el-option
                    :label="item.label"
                    :value="item.id"
                    v-for="(item, index) in areaList"
                    :key="index"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="详细地址" prop="">
                <el-input
                  v-model="form.address"
                  placeholder="请输入"
                  clearable
                />
              </el-form-item>
            </el-col>
          </span>

          <span v-else>
            <el-col :span="16">
              <el-form-item label="单位地址" prop="">
                <el-input v-model="form.addressDetails" />
              </el-form-item>
            </el-col>
          </span>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button type="primary" v-if="status != '3'" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  manufactureList,
  manufactureListing,
  manufactureExport,
  manufactureIdDetail,
  manufactureAdd,
  manufactureEdit,
  manufactureDelete,
} from "@/api/basicData/manufacture";
import { areaList, areadeptTree, getInfoarea } from "@/api/area";

export default {
  name: "manufacture",
  dicts: ["equipment_state"],
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      rules: {
        manufactureName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        contactsMobile: [
          {
            pattern:
              /(^\d{3,4}-\d{7,8}(-\d{3,4})?$)|(^1[3|4|5|6|7|8|9][0-9]\d{8}$)/,
            message: "请输入正确的固定电话或手机号",
            trigger: "blur",
          },
        ],
      },
      // 视图高度
      height: document.documentElement.clientHeight,
      // 厂家列表
      manufactureList: [],
      // 表单参数
      form: {},
      loading: false,
      status: null,
      title: null,
      // 省列表
      provinceList: [],
      // 市列表
      cityList: [],
      // 区县列表
      areaList: [],
      // 厂家下拉框
      manufactureSelect: [],
    };
  },
  created() {
    this.getList();
    // 获取省
    this.getProvinceList();
  },
  filters: {
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 10) {
        return value.slice(0, 10) + "...";
      }
      return value;
    },
  },
  methods: {
    // 获取场所列表
    getList() {
      this.queryParams.createTimeStart =
        this.queryParams.formDate && this.queryParams.formDate[0];
      this.queryParams.createTimeEnd =
        this.queryParams.formDate && this.queryParams.formDate[1];
      let queryParams = { ...this.queryParams };
      delete queryParams.formDate;
      this.loading = true;
      manufactureList(queryParams)
        .then((response) => {
          this.manufactureList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 获取省列表
    getProvinceList() {
      areaList()
        .then((response) => {
          this.provinceList = response.data;
        })
        .catch((response) => {});
    },
    // 更改省
    changeProvince(val, isview) {
      console.log("val", val);
      if (!isview) {
        this.$set(this.form, "cityCode", null);
        this.$set(this.form, "areaCode", null);
      }
      this.cityList = [];
      this.areaList = [];
      areaList({ parentId: val })
        .then((response) => {
          this.cityList = response.data;
        })
        .catch((response) => {});
    },
    // 更改市
    changeCity(val, isview) {
      console.log("val", val);
      if (!isview) {
        this.$set(this.form, "areaCode", null);
      }
      this.areaList = [];
      areaList({ parentId: val })
        .then((response) => {
          this.areaList = response.data;
        })
        .catch((response) => {});
    },
    // 打开对话框
    openDialog(row, status) {
      console.log("子row", row);
      this.resetFormmy();
      if (row) {
        manufactureIdDetail(row.manufactureId).then((response) => {
          this.form = response.data;
          if (status != "3") {
            if (this.form.provinceCode) {
              this.changeProvince(this.form.provinceCode, true);
            }
            if (this.form.cityCode) {
              this.changeCity(this.form.cityCode, true);
            }
          }
          this.dialogVisible = true;
        });
        if (status == "3") {
          this.status = "3";
          this.title = "查看厂家";
        } else {
          this.status = "2";
          this.title = "编辑厂家";
        }
      } else {
        this.status = "1";
        this.title = "添加厂家";
        this.dialogVisible = true;
      }
    },
    // 重置表单
    resetFormmy() {
      this.form = {};
      this.resetForm("form");
      this.cityList = [];
      this.areaList = [];
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) return;
        if (this.status == "1") {
          manufactureAdd(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.dialogVisible = false;
            this.getList();
          });
        } else if (this.status == "2") {
          manufactureEdit(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm(`是否确认删除厂家[${row.manufactureName}]？`)
        .then(() => {
          this.loading = true;
          return manufactureDelete(row.manufactureId);
        })
        .then(() => {
          this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 重置查询
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryParams");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.operation {
  margin-block: 30px;
}
</style>
